package nio.base;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

import org.junit.Test;

public class BigFileTest {

    public static void main(String[] args) throws IOException {

        try (
                FileInputStream in = new FileInputStream("C:/Users/tuyou/Downloads/CentOS-7.0-1406-x86_64-DVD.iso");
                FileChannel channel = in.getChannel();
        ) {
            ByteBuffer bb = ByteBuffer.allocate(1024 * 1024);
            while (channel.position() < channel.size()) {
                int count = channel.read(bb);
                bb.flip();
                System.out.println(count);
                bb.clear();
            }
        }
    }

    @Test
    public void point() throws FileNotFoundException, IOException {

        try (
                FileInputStream in = new FileInputStream("in.edge");
                FileOutputStream out = new FileOutputStream("out.edge");
                FileChannel inChannel = in.getChannel(); //只读channel
                FileChannel outChannel = out.getChannel(); //只写channel
        ) {
            ByteBuffer bb = ByteBuffer.allocate(1024);
            bb.put("sf".getBytes());
            inChannel.read(bb);
            bb.flip();
            outChannel.write(bb);
        }
    }

    //可以对一个文件开启两个写
    @Test
    public void doubleOutStream() throws FileNotFoundException, IOException {

        try (
                FileOutputStream out = new FileOutputStream("out.edge", true);
                FileOutputStream out2 = new FileOutputStream("out.edge", true);
                FileChannel outChannel = out.getChannel();
                FileChannel out2Channel = out2.getChannel();
        ) {
            ByteBuffer bb = ByteBuffer.allocate(1024);
            bb.put("sf".getBytes());
            bb.flip();
            outChannel.position(outChannel.size());
            outChannel.write(bb);
            out2Channel.position(outChannel.position());
            bb.rewind();
            out2Channel.write(bb);
        }
    }

    //测试证明，对一个文件同时两个rw都是都是可以的
    @Test
    public void doubleRandomAccessFile() throws FileNotFoundException, IOException {

        try (
                RandomAccessFile out = new RandomAccessFile("out.edge", "rw");
                RandomAccessFile out2 = new RandomAccessFile("out.edge", "rw");
                FileChannel outChannel = out.getChannel(); //可以对一个文件开启两个写
                FileChannel out2Channel = out2.getChannel(); //可以对一个文件开启两个写
        ) {
            ByteBuffer bb = ByteBuffer.allocate(1024);
            bb.put("sf".getBytes());
            bb.flip();
            outChannel.position(outChannel.size());
            outChannel.write(bb);
            out2Channel.position(outChannel.position());
            bb.rewind();
            out2Channel.write(bb);
        }
    }
}
